(defpackage rollover-selection
  (:use :cl)
  (:nicknames :rs)
  (:export :rollover-selection-mixin))

(in-package :rollover-selection)

(defclass rollover-selection-mixin (bt:basic-trial)
  ())

(exp:define-action highlight-node ((trial rollover-selection-mixin)
                                   (e:mouse-motion :var motion)
                                   (bt:select-mode))
  (labels ((get-node (node)
             (if (and (typep node 'd:argument)
                      (d:quantity node))
                 (tree:parent node)
                 node)))
    (let ((leave (when (getf motion :leave)
                   (d:find-id-or-die (d:root (p:diagram trial)) (getf motion :leave))))
          (enter (when (getf motion :enter)
                   (d:find-id-or-die (d:root (p:diagram trial)) (getf motion :enter)))))
      (when leave
        (setf (d:highlighted (get-node leave)) nil))
      (when enter
        (setf (d:highlighted (get-node enter)) t))
      ;; log the action
      (cond
        ((and leave
              (not enter))
         (exp:lg 'leave :node (d:id (get-node leave))))
        ((and enter
              (not leave))
         (exp:lg 'enter :node (d:id (get-node enter))))
        ((and enter
              leave
              (not (eq (get-node enter)
                       (get-node leave))))
         (exp:lg 'leave :node (d:id (get-node leave)))
         (exp:lg 'enter :node (d:id (get-node enter)))))
      (when (or enter
                leave)
        (setf (gwee:view-needs-update-p (p:diagram trial)) t)))))
